<?php
require_once "dispose.interface.php";
/**
 *
* Copyright(c) 201x,
* All rights reserved.
*
* 功 能：todo 可能会用到的
* @author cabing_2005@126.com
* @date 2014-8-15
* 版 本：1.0
 */
class MongoDisposeException extends  Exception{}
class MongoDispose implements Dispose{
    public static function  dispose($params){

    }
}

// conncect to mongo db
class MongodbAdapter
{

    private $_mongo = null;
    private $_mongoDb = null;
    private $_lastInsertId = null;


    /**
     * Get Dsn
     *
     * @return string
     */
    public function getDsn()
    {
        //mongodb://${username}:${password}@localhost/blog

        $dsn = '';
        if (strtolower($this->config['schema']) != 'mongodb') {
            throw new Exception("数据库配置缺少关键字段schema");
        }
        $dsn .= 'mongodb://';

        if (!isset($this->config['password'])) {
            $this->config['password'] = '';
        }
        if (isset($this->config['username']) && $this->config['username'] != '') {
            $dsn .= $this->config['username'] . ':' . $this->config['password'] . '@';
        }
        if ($this->config['host']) {
            $dsn .= $this->config['host'];
        }
        if ($this->config['port']) {
            $dsn .= ':' . $this->config['port'];
        }
        if ($this->config['dbname']) {
            $dsn .= '/' . $this->config['dbname'];
        }
        return $dsn;
    }
    /**
     * Constructor
     *
     * @param array $config array
     *
     * @return void
     */
    public function __construct($config)
    {
        $this->setStatementClass('MongoCursor');
        if (is_array($config)) {
            $this->config = $config;
            $this->connect();
        }
    }

    /**
     * Creates a Mongo object and connects to the database.
     *
     * @return void
     * @throws Exception
     */
    public function connect()
    {
        if ($this->connection) {
            return;
        }

        try
        {
            $this->_mongo = new Mongo($this->getDsn());
        } catch (Exception $e)
        {
            throw new Exception(
                sprintf('can\'t connected mongodb, the dsn: %s', $this->getDsn())
            );
        }

        if ($this->_mongo instanceof Mongo) {

            $this->connection = $this->_mongo;

            $this->_mongoDb = $this->selectDb($this->config['dbname']);
        }
    }
}









